<?php



/**
 * Forum
 */
class Forum
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $title
     */
    private $title;

    /**
     * @var text $description
     */
    private $description;

    /**
     * @var string $image
     */
    private $image;

    /**
     * @var boolean $access
     */
    private $access;

    /**
     * @var integer $pos
     */
    private $pos;

    /**
     * @var ForumsCategory
     */
    private $category;

    /**
     * @var Group
     */
    private $groups;
    /**
     * @var ForumsDiscussion
     */
    private $discussions;

    public function __construct()
    {
        $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * Get title
     *
     * @return string $title
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set description
     *
     * @param text $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * Get description
     *
     * @return text $description
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set image
     *
     * @param string $image
     */
    public function setImage($image)
    {
        $this->image = $image;
    }

    /**
     * Get image
     *
     * @return string $image
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * Set access
     *
     * @param boolean $access
     */
    public function setAccess($access)
    {
        $this->access = $access;
    }

    /**
     * Get access
     *
     * @return boolean $access
     */
    public function getAccess()
    {
        return $this->access;
    }

    /**
     * Set pos
     *
     * @param integer $pos
     */
    public function setPos($pos)
    {
        $this->pos = $pos;
    }

    /**
     * Get pos
     *
     * @return integer $pos
     */
    public function getPos()
    {
        return $this->pos;
    }

    /**
     * Set category
     *
     * @param ForumsCategory $category
     */
    public function setCategory(\ForumsCategory $category)
    {
        $this->category = $category;
    }

    /**
     * Get category
     *
     * @return ForumsCategory $category
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Add groups
     *
     * @param Group $groups
     */
    public function addGroups(\Group $groups)
    {
        $this->groups[] = $groups;
    }

    /**
     * Get groups
     *
     * @return Doctrine\Common\Collections\Collection $groups
     */
    public function getGroups()
    {
        return $this->groups;
    }


    /**
     * Add discussions
     *
     * @param ForumsDiscussion $discussions
     */
    public function addDiscussions(\ForumsDiscussion $discussions)
    {
        $this->discussions[] = $discussions;
    }

    /**
     * Get discussions
     *
     * @return Doctrine\Common\Collections\Collection $discussions
     */
    public function getDiscussions()
    {
        return $this->discussions;
    }

    /**
     * Indique si l'on a le droit de voir le forum ou non
     * @param session courante
     */
    public function hasCredential($_session) {
        if ( !$this->access ) return true; // Si pas d'accès = visible
        if ( !$_session->isAuthenticated() ) return false; // Accès demandé, pas connecté = pas visible
        if ( $this->groups->isEmpty() ) return true; // Accès demandé, connecté, pas de groupe = visible

        // Accès demandé, connecté, groupes
        foreach ($this->groups AS $group) {
            foreach ($_session->getUser()->getGroups() AS $groupUser) {
                if ( $group->getId() == $groupUser->getId() ) {
                    return true; // Si l'un des groupes de l'utilisateur match = visible
                }
            }
        }
        return false; // si aucun groupe ne match = pas visible
    }

    public function isDiscussionNotRead($_session) {
        if ( !$_session->isAuthenticated() ) {
            return false;
        }
        
        foreach($this->discussions AS $discussion) {
            if ( !$discussion->isRead($_session) ) {
                return true;
            }
        } 
        return false;
    }

}